#ifndef _QUANTUM_EVENT_POLL_H__
#define _QUANTUM_EVENT_POLL_H__

#include "curses.h"
#include "event.h"
#include "type.h"
#include <cassert>
#include <queue>
#include <unordered_map>

static inline std::queue<Event> events;

const static std::unordered_map<EventType, type::string> type_map{
	{EventType::KeyDown, _("EventType::KeyDown")},
	{EventType::MouseMove, _("EventType::MouseMove")},
	{EventType::Resize, _("EventType::Resize")},
	{EventType::None, _("EventType::None")}};

class EventPoll {
  public:
	/// get event from events' queue
	static Event get_event() {
		Event e;
		if (events.empty()) {
			e.event = EventType::None;
			return e;
		}
		e = events.front();
		events.pop();
		return e;
	}
	static void pull_event() {
		type::input_type ch;

#ifdef UNICODE
		get_wch(&ch);
#else
		ch = getch();
#endif // UNICODE

		Event e{};
		switch (ch) {
		case KEY_MOUSE:
			e.event = EventType::MouseMove;
			e.mouse = {0};
			assert(getmouse(&e.mouse) == OK);
			break;
		case KEY_RESIZE:
			e.event = EventType::Resize;
			break;
		default:
			e.event = EventType::KeyDown;
			e.key = ch;
		}
		events.push(e);
	}
};

#endif // _QUANTUM_EVENT_POLL_H__
